<?php
class UserAction extends AgriculturalproductsCommonAction {

	// 网站首页
	public function index() {
		$this->display();
	}
	
	public function resetPassword(){		
		$condition['email'] = $_REQUEST['email'] ;
		$emailVo = $this->simpleQuery($condition,'loginuser',1);		
	
		
		if(empty($emailVo['email'])){
			$this->assign("waitSecond",3);
			$this->success("注册邮箱不存在，请重新输入！") ;			
		}else{
			
			$mailMsg = "<p>尊敬的用户：</p>" . 
					   "<p>".
						"        " . $emailVo['name']." ,您好！ </p>".
						"<p>        您的密码为: " . $emailVo['pwd'] .  " </p>".
						"<p>        您现在可以使用此密码登录万州特色农产品网。 </p>".
						"<p style='margin-left:250px;'><br/>万州特色农产品网 ".
						"<br/>" . date('Y-m-d H:i:s',time()) . "</p>".
						"<p>此邮件为自动发送，请勿回复！</p>" ;
			
			SendMail($emailVo['email'],"邮件标题",$mailMsg);
			$this->assign("jumpUrl","__APP__/User/login");
			$this->assign("waitSecond",3);
			$this->success ( "已成功将密码发送至您的邮箱中，请注意查收！" );
		}
	}
	
	/**
	 * 用户注销
	 * Enter description here ...
	 */
	public function loginout(){

		if(Session::is_set(C('USER_AUTH_KEY'))){
			//Session::destroy() ;
			unset($_SESSION[C('USER_AUTH_KEY')]);
			$this->assign("jumpUrl","__APP__/");
			$this->success('登出成功！');
		}else{
			$this->success('已经登出！');
		}

	}
	
	public function verify(){
		$type =	isset($_GET['type'])?$_GET['type']:'gif';
		import("@.ORG.Image");
		Image::buildImageVerify(4,1,$type);
	}

	/**
	 * 
	 * 用户登陆
	 */
	public function userLogin(){
		if($_SESSION['verify'] != md5($_REQUEST['verify'])) {			
			$this->error('验证码错误！');
		}
		$Dao = M("loginuser");			
		// 构造查询条件
		$condition ["name"] = $_REQUEST['username'];
		$condition ["pwd"] = $_REQUEST['password'];
		// 查询数据
		$list = $Dao->where($condition)->find();

		if($list){
			$this->assign("jumpUrl","__APP__/Index/");
			Session::set(C('USER_AUTH_KEY'),$_REQUEST['username']);
			$this->success ( "登陆成功！" );
		}else{
			$this->success("用户名或者密码错误，请重新输入！") ;
		}
	}
	

	/**
	 * 
	 * 用户注册
	 */
	public function postinfo(){
		if($_SESSION['verify'] != md5($_REQUEST['verify'])) {
			$this->error('验证码错误！');
		}
		$model = M ("loginuser");
		//保存当前数据对象
		$data ["name"] = $_REQUEST['username'];
		$data ["pwd"] = $_REQUEST['password'];
		$data ["realname"] = $_REQUEST['realname'];
		$data ["email"] = $_REQUEST['email'];
		$data ["mobile"] = $_REQUEST['mobile'];
		$data ["address"] = $_REQUEST['address'];
		
		$data ["createdate"] = date('Y-m-d H:i:s',time());
		$data ["statusdate"] = date('Y-m-d H:i:s',time());
		$data ["status"] = 1;
		
		
		$lastInsId = $model->add($data);
		if ($lastInsId != -1) {
			//注册后跳转至首页
			$this->assign("jumpUrl","__APP__/Index/");
			//设置session
			Session::set(C('USER_AUTH_KEY'),$_REQUEST['username']);
			//Session::set('username',$_REQUEST['username']);
			$this->success ( "新增用户成功" );
		}else{
			$this->success($model->getError ()) ;
		}
	}
	
	

	
	public function uservalidate(){
		$user = $_REQUEST["username"];
		if($user == ""){
			echo "用户名不能为空！";
			return;
		}
		
		$model = D ( "loginuser" );
		// 构造查询条件
		$condition['name'] = $user;
		// 查询数据
		$list = $model->where($condition)->find();
		if(count($list)>0){
			//$this->ajaxReturn ( "111111", "已存在相同的用户，请选择其它用户名进行注册!", 1 );
			echo("已存在相同的用户，请选择其它用户名进行注册!");
		}else{
			//$this->ajaxReturn ( "111111", "可以使用这个用户进行注册!", 1 );
			//echo("可以使用这个用户进行注册!");
			echo "success";
		}
	}


}
?>